home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / HTML and CSS Modes / htmlMoveFiles.tcl < prev    next >
Encoding:
Text File  |  2001-01-14  |  21.8 KB  |  616 lines

  1. ## -*-Tcl-*-
  2.  # ###################################################################
  3.  #  HTML mode - tools for editing HTML documents
  4.  # 
  5.  #  FILE: "htmlMoveFiles.tcl"
  6.  #                                    created: 99-07-20 18.26.09 
  7.  #                                last update: 01-01-14 18.52.32 
  8.  #  Author: Johan Linde
  9.  #  E-mail: <alpha_www_tools@go.to>
  10.  #     www: <http://go.to/alpha_www_tools>
  11.  #  
  12.  # Version: 3.0
  13.  # 
  14.  # Copyright 1996-2001 by Johan Linde
  15.  #  
  16.  # This program is free software; you can redistribute it and/or modify
  17.  # it under the terms of the GNU General Public License as published by
  18.  # the Free Software Foundation; either version 2 of the License, or
  19.  # (at your option) any later version.
  20.  # 
  21.  # This program is distributed in the hope that it will be useful,
  22.  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  # GNU General Public License for more details.
  25.  # 
  26.  # You should have received a copy of the GNU General Public License
  27.  # along with this program; if not, write to the Free Software
  28.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  # 
  30.  # ###################################################################
  31.  ##
  32.  
  33. #===============================================================================
  34. # This file contains procs for moving and renaming files.
  35. #===============================================================================
  36.  
  37. proc html::RenameFile {} {
  38.     html::MoveFiles 1
  39. }
  40.  
  41. proc html::RenameFolder {} {
  42.     html::MoveFiles 1 1
  43. }
  44.  
  45. # Moves files from one folder to another and update all links to the moved files
  46. # as well as all links in the moved files.
  47. proc html::MoveFiles {{rename 0} {renamefolder 0}} {
  48.     global HTMLmodeVars
  49.     
  50.     set mrtxt {moved renamed}
  51.     set filfol {"Files have" "Folder has"}
  52.     # Check that a home page is defined.
  53.     if {![html::IsThereAHomePage]} {return}
  54.     
  55.     if {[html::AllSaved "{All windows must be saved before you can move files. Save?}"] == "no"} {return}
  56.  
  57.     if {$rename} {
  58.         if {$renamefolder} {
  59.             set fromFolder [html::GetDir "Rename."]
  60.             set mf [file::recurse $fromFolder]
  61.             set movefiles ""
  62.             foreach mm $mf {
  63.                 lappend movefiles [string range $mm [expr {[string length $fromFolder] + 1}] end]
  64.             }
  65.         } else {
  66.             set movefiles [getfile "Select file to rename."]
  67.             set fromFolder [file dirname $movefiles]
  68.             set movefiles [list [file tail $movefiles]]
  69.         }
  70.     } else {
  71.         set fromFolder [html::GetDir "Move from."]
  72.     }
  73.     
  74.     set base [html::BASEfromPath $fromFolder]
  75.     # Is this folder in a home page folder?
  76.     if {[lindex $base 0] == "file:///"} {
  77.         alertnote "'[file tail $fromFolder]' is not in a home page folder or an include folder."
  78.         return 
  79.     }
  80.     set fromPath [lindex $base 1]
  81.     set homepage [lindex $base 3]
  82.     set fromBase [lindex $base 0]
  83.     set isInInclFldr [lindex $base 4]
  84.     set inclFld [lindex $base 5]
  85.     
  86.     # Check that the corresponding include or home page folder exists.
  87.     if {$isInInclFldr} {
  88.         if {![file isdirectory $homepage]} {
  89.             alertnote "Could not find the corresponding home page folder for\
  90.             ${fromBase}$fromPath. Fix that and try again."
  91.             html::HomePages "${fromBase}$fromPath"
  92.             return
  93.         }
  94.     } elseif {$inclFld != "" && ![file isdirectory $inclFld]} {
  95.         alertnote "Could not find the corresponding include folder for\
  96.         ${fromBase}$fromPath. Fix that and try again."
  97.         html::HomePages "${fromBase}$fromPath"
  98.         return
  99.     }
  100.     
  101.     # Get files to move.
  102.     if {!$rename} {
  103.         set files [glob -nocomplain -dir $fromFolder *]
  104.         foreach f $files {
  105.             if {![file isdirectory $f]} {
  106.                 lappend filelist [file tail $f]
  107.             }
  108.         }
  109.         if {![info exists filelist]} {
  110.             alertnote "Empty folder."
  111.             return
  112.         }
  113.     
  114.         if {[catch {listpick -p "Select files to move." -l $filelist} movefiles] || \
  115.           ![string length $movefiles]} {return}
  116.     
  117.         
  118.         # Get folder to move to.
  119.         if {[catch {html::GetDir "Move to."} toFolder]} {return}
  120.         if {$fromFolder == $toFolder} {
  121.             alertnote "This is the same folder as you moved from."
  122.             return
  123.         }
  124.     }
  125.  
  126.     if {$renamefolder} {
  127.         set toFolder [putfile "New folder name." [file tail $fromFolder]]
  128.     } elseif {$rename} {
  129.         set newname [lindex $movefiles 0]
  130.         while {$newname == [lindex $movefiles 0]} {
  131.             set newname [putfile "New filename." $newname]
  132.             set toFolder [file dirname $newname]
  133.             set newname [file tail $newname]
  134.             if {$newname == [lindex $movefiles 0]} {alertnote "You must give the file a new name."}
  135.         }
  136.     }
  137.     
  138.     # Is this folder in the same home page folder?
  139.     if {!$isInInclFldr && ![string match [file join $homepage *] [file join $toFolder " "]] ||
  140.     $isInInclFldr && ![string match [file join $inclFld *] [file join $toFolder " "]]} {
  141.         set msg {"home page" "" "" "" "include"}
  142.         alertnote "'[file tail $toFolder]' is not in the same [lindex $msg $isInInclFldr] folder."
  143.         return
  144.     }
  145.         
  146.     # Move the files.
  147.     foreach f $movefiles {
  148.         if {$rename && !$renamefolder} {
  149.             set f1 $newname
  150.         } else {
  151.             set f1 $f
  152.         }
  153.         if {[file exists [file join $toFolder $f1]]} {
  154.             if {$rename || [askyesno "Replace '$f1' in folder '[file tail $toFolder]'?"] == "yes"} {
  155.                 file delete [file join $toFolder $f1]
  156.             } else {
  157.                 continue
  158.             }
  159.         }
  160.         set reo 0
  161.         foreach w [html::AllWindowPaths] {
  162.             if {[stripNameCount $w] == [file join $fromFolder $f]} {
  163.                 alertnote "'[file tail $w]' must be closed before it can be [lindex $mrtxt $rename]. It will be reopened again."
  164.                 bringToFront $w
  165.                 killWindow
  166.                 set reo 1
  167.             }
  168.         }
  169.         if {!$renamefolder && [catch {file rename [file join $fromFolder $f] [file join $toFolder $f1]}] && \
  170.           ![file exists [file join $toFolder $f]]} {
  171.             alertnote "Could not move $f. An error occurred."
  172.             if {$reo} {lappend reOpen [file join $fromFolder $f]}
  173.         } else {
  174.             lappend movedFiles [file join $fromFolder $f]
  175.             lappend movedFiles2 [file join $toFolder $f1]
  176.             if {$reo} {lappend reOpen [file join $toFolder $f1]}
  177.         }
  178.     }
  179.     
  180.     if {$renamefolder && [catch {file rename $fromFolder $toFolder}]} {
  181.         alertnote "Could not rename $fromFolder. An error occurred."
  182.     } elseif {[info exists movedFiles] && [lindex [dialog -w 400 -h 70 -t "[lindex $filfol $renamefolder] been [lindex $mrtxt $rename]. Update links?" \
  183.       10 10 290 30 -b Update 20 40 85 60 -b Cancel 105 40 170 60] 0]} {
  184.         if {$isInInclFldr} {
  185.             # Update include links in home page files pointing to moved files in include folder.
  186.             set allFiles [html::AllHTMLfiles $homepage]
  187.             set x [html::UpdateAfterMove3 $movedFiles $movedFiles2 $homepage $inclFld $allFiles]
  188.             set num [lindex $x 0]
  189.             set changed [lindex $x 1]
  190.             # Update include links in include files pointing to moved files in include folder.
  191.             set allFiles [html::AllHTMLfiles $inclFld 0 $movedFiles2]
  192.             set x [html::UpdateAfterMove3 $movedFiles $movedFiles2 $homepage $inclFld $allFiles]
  193.             incr num [lindex $x 0]
  194.             set changed [concat $changed [lindex $x 1]]
  195.             # Update include links in moved files.
  196.             set filelist [html::OpenAfile]
  197.             set fid [lindex $filelist 0]
  198.             foreach ff $movedFiles2 {
  199.                 puts $fid $ff
  200.             }
  201.             close $fid
  202.             set x [html::UpdateAfterMove3 $movedFiles $movedFiles2 $homepage $inclFld [lindex $filelist 1] 1]
  203.             set changed [concat $changed [lindex $x 1]]            
  204.         } else {
  205.             # Update links in home page files pointing to moved files in home page folder.
  206.             set x [html::UpdateAfterMove $movedFiles $movedFiles2 $fromBase $fromPath $homepage $homepage]
  207.             set num [lindex $x 0]
  208.             set changed [lindex $x 1]
  209.             # Update links in moved files.
  210.             incr num [html::UpdateAfterMove2 $movedFiles $movedFiles2 $fromBase $fromPath $homepage]
  211.             # Update links in include folder files pointing to moved files in home page folder.
  212.             if {$inclFld != ""} {
  213.                 set x [html::UpdateAfterMove $movedFiles $movedFiles2 $fromBase $fromPath $homepage $inclFld 1]
  214.                 incr num [lindex $x 0]
  215.                 set changed [concat $changed [lindex $x 1]]
  216.             }
  217.             # Update include links in home page files pointing to moved files in home page folder.
  218.             set allFiles [html::AllHTMLfiles $homepage 0 $movedFiles2]
  219.             set x [html::UpdateAfterMove3 $movedFiles $movedFiles2 $homepage $inclFld $allFiles]
  220.             incr num [lindex $x 0]
  221.             set changed [concat $changed [lindex $x 1]]
  222.             # Update include links in moved files.
  223.             set filelist [html::OpenAfile]
  224.             set fid [lindex $filelist 0]
  225.             foreach ff $movedFiles2 {
  226.                 puts $fid $ff
  227.             }
  228.             close $fid
  229.             set x [html::UpdateAfterMove3 $movedFiles $movedFiles2 $homepage $inclFld [lindex $filelist 1] 1]
  230.             set changed [concat $changed [lindex $x 1]]            
  231.         }
  232.     }
  233.     
  234.     # BUG!!! Make sure $num is correct. Files can be double counted.
  235.     catch {message "$num files has been modified including the ones [lindex $mrtxt $rename]."}
  236.  
  237.     if {[info exists reOpen] && [askyesno "Reopen previously closed windows?"] == "yes"} {
  238.         foreach r $reOpen {
  239.             edit $r
  240.         }
  241.     }
  242.     
  243.     if {[llength $changed]} {
  244.         foreach r [lunique $changed] {
  245.             bringToFront $r
  246.             revert
  247.         }
  248.     }
  249. }
  250.  
  251. # Updates links to moved files.
  252. proc html::UpdateAfterMove {movedFiles movedFiles2 fromBase fromPath homepage isinfld {inclfiles 0}} {
  253.     
  254.     set allfiles [html::AllHTMLfiles $isinfld 1 $movedFiles2]
  255.     
  256.     # Build regular expressions with URL attrs.
  257.     set exprr [html::URLregexp]
  258.     set exprr2 {(url)\([ \t\r\n]*("[^"]+"|'[^']+'|[^ \t\n\r\)]+)[ \t\r\n]*\)}
  259.  
  260.     # Update links to the moved files.
  261.     set toModify [html::ScanFiles $allfiles $fromBase $fromPath $homepage $isinfld 0 0 $movedFiles]
  262.     set fidr [lindex $toModify 0]
  263.     seek $fidr 0
  264.     set num 0
  265.     set changed ""
  266.     set thisfile ""
  267.     while {![eof $fidr]} {
  268.         gets $fidr modify
  269.         if {$modify == ""} {continue}
  270.  
  271.         set fil [lindex $modify 0]
  272.         if {$thisfile != $fil} {
  273.             if {[string length $thisfile]} {
  274.                 if {[catch {open $thisfile w} fid]} {
  275.                     alertnote "Could not update [file tail $thisfile]. An error occurred."
  276.                 } else {
  277.                     puts -nonewline $fid [join $filecont "\r"]
  278.                     close $fid
  279.                 }
  280.             }
  281.             message "Modifying [file tail $fil]…"
  282.             foreach w [html::AllWindowPaths] {
  283.                 if {[stripNameCount $w] == "$fil"} {
  284.                     lappend changed $w
  285.                 }
  286.             }
  287.             set fid [open $fil r]
  288.             incr num
  289.             set filec [read $fid]
  290.             close $fid
  291.             if {[regexp {\n} $filec]} {
  292.                 set newln "\n"
  293.             } else {
  294.                 set newln "\r"
  295.             }
  296.             set filec [split $filec $newln]
  297.             set filecont ""
  298.             foreach fc $filec {
  299.                 lappend filecont [string trimleft $fc "\r"]
  300.             }
  301.         }
  302.         set thisfile $fil
  303.         set linenum [expr {[lindex $modify 1] - 1}]
  304.         set line [lindex $filecont $linenum]
  305.         set path [lindex $movedFiles2 [lsearch -exact $movedFiles [lindex $modify 5]]]
  306.         set lnk [html::BASEfromPath $path]
  307.         if {$inclfiles && "[lindex $modify 2][lindex $modify 3]" == "[lindex $lnk 0][lindex $lnk 1]"} {
  308.             set linkTo ":HOMEPAGE:[lindex $lnk 2]"
  309.         } elseif {[lindex $modify 2] == [lindex $lnk 0]} {
  310.             set linkTo [html::RelativePath "[lindex $modify 3][lindex $modify 4]" "[lindex $lnk 1][lindex $lnk 2]"]
  311.         } else {
  312.             set linkTo [join [lrange $lnk 0 2] ""]
  313.         }
  314.         set linkTo [html::URLescape2 $linkTo]
  315.         set tomod [quote::Regfind [lindex $modify 6]]
  316.         set ii $linenum
  317.         set maxnline [llength $filecont]
  318.         while {![regexp -indices "$tomod" $line href] && $ii < $maxnline} {
  319.             append line "" [lindex $filecont [incr ii]]
  320.         }
  321.         regsub -all "" $line "\r" line
  322.         # This shouldn't fail!
  323.         if {[regexp -nocase -indices $exprr [string range $line [lindex $href 0] [lindex $href 1]] a b url] ||
  324.         [regexp -nocase -indices $exprr2 [string range $line [lindex $href 0] [lindex $href 1]] a b url]} {
  325.             set anchor ""
  326.             regexp {[^#]*(#[^\"]*)} $tomod a anchor
  327.             set line "[string range $line 0 [expr {[lindex $href 0] + [lindex $url 0] - 1}]]\"$linkTo$anchor\"[string range $line [expr {[lindex $href 0] + [lindex $url 1] + 1}] end]"
  328.             set filecont [eval lreplace [list $filecont] $linenum $ii [split $line "\r"]]
  329.         } else {
  330.             alertnote "An error occured when updating a link in [file tail $thisfile]."
  331.         }
  332.     }
  333.     if {$thisfile != ""} {
  334.         if {[catch {open $thisfile w} fid]} {
  335.             alertnote "Could not update [file tail $thisfile]. An error occurred."
  336.         } else {
  337.             puts -nonewline $fid [join $filecont "\r"]
  338.             close $fid
  339.         }
  340.     }
  341.     close $fidr
  342.     catch {file delete [lindex $toModify 1]}
  343.     return [list $num $changed]
  344. }
  345.  
  346. # Updates links in moved files.
  347. proc html::UpdateAfterMove2 {movedFiles movedFiles2 fromBase fromPath homepage} {
  348.     global file::separator
  349.     
  350.     set expBase "<(base\[ \\t\\n\\r\]+)\[^>\]*>"
  351.     set expBase2 "(href\[ \\t\\n\\r\]*=\[ \\t\\n\\r\]*)(\"\[^\"\]+\"|'\[^'\]+'|\[^ \\t\\n\\r>\]+)"
  352.  
  353.     # Build regular expressions with URL attrs.
  354.     set exprr1 "<!--|<\[^<>\]+\[ \\t\\n\\r\]+[html::URLregexp]"
  355.     set exprr2 {/\*|[ \t\r\n]+(url)\([ \t\r\n]*("[^"]+"|'[^']+'|[^ \t\n\r\)]+)[ \t\r\n]*\)}
  356.     set commStart1 "<!--"
  357.     set commEnd1 "-->"
  358.     set commStart2 {/*}
  359.     set commEnd2 {\*/}
  360.  
  361.     set num 0
  362.     foreach f $movedFiles2 {
  363.         getFileInfo $f finfo
  364.         if {$finfo(type) != "TEXT"} {continue}
  365.         message "Modifying [file tail $f]…"
  366.         set created $finfo(created)
  367.         set fid [open $f r]
  368.         set filecont [read $fid 16384]
  369.         set limit [expr {[eof $fid] ? 0 : 300}]
  370.         set temp [html::OpenAfile]
  371.         set tempf [lindex $temp 1]
  372.         set tempfid [lindex $temp 0]
  373.         set oldfile [lindex $movedFiles [lsearch -exact $movedFiles2 $f]]
  374.         set base $fromBase
  375.         set path $fromPath
  376.         set hpPath $homepage
  377.         set epath [string range $oldfile [expr {[string length $homepage] + 1}] end]
  378.         regsub -all ${file::separator} $epath {/} epath
  379.         # Replace newline chars in IBM files.
  380.         regsub -all "\n\r" $filecont "\r" filecont
  381.         # If BASE is used, only modify links to moved files.
  382.         set hasBase 0
  383.         if {[regexp -nocase -indices $expBase $filecont this]} {
  384.             set preBase [string range $filecont 0 [lindex $this 0]]
  385.             set comm 0
  386.             while {[regexp -indices {<!--} $preBase bCom]} {
  387.                 set preBase [string range $preBase [expr {[lindex $bCom 1] - 1}] end]
  388.                 set comm 1
  389.                 if {[regexp -indices -- {-->} $preBase bCom]} {
  390.                     set preBase [string range $preBase [expr {[lindex $bCom 1] - 1}] end]
  391.                     set comm 0
  392.                 } else {
  393.                     break
  394.                 }
  395.             }
  396.             if {!$comm && [regexp -nocase $expBase2 [string range $filecont [lindex $this 0] [lindex $this 1]] d1 d2 url1]} {
  397.                 set url1 [string trim $url1 "\"' \t\r\n"]
  398.                 set hasBase 1
  399.             }
  400.         }
  401.         if {$hasBase && ![catch {html::BASEpieces $url1} basestr]} {
  402.             set base [lindex $basestr 0]
  403.             set path [lindex $basestr 1]
  404.             set epath [lindex $basestr 2]
  405.             set hpPath ""
  406.         }
  407.         incr num
  408.         for {set i1 1} {$i1 < 3} {incr i1} {
  409.             if {$i1 == 2} {
  410.                 close $fid
  411.                 seek $tempfid 0
  412.                 set fid $tempfid
  413.                 set filecont [read $fid 16384]
  414.                 set limit [expr {[eof $fid] ? 0 : 300}]
  415.                 set temp [html::OpenAfile]
  416.                 set tempfid [lindex $temp 0]
  417.             }
  418.             set commStart [set commStart$i1]
  419.             set commEnd [set commEnd$i1]
  420.             set exprr [set exprr$i1]
  421.             set comment 0
  422.             while {1} {
  423.                 while {$comment || ([regexp -nocase -indices $exprr $filecont href b url] &&
  424.                 [expr {[string length $filecont] - [lindex $href 0]}] > $limit)} {
  425.                     # Comment?
  426.                     if {$comment || [string range $filecont [lindex $href 0] [lindex $href 1]] == $commStart} {
  427.                         if {$comment} {
  428.                             set href {0 0}
  429.                             set subcont $filecont
  430.                         } else {
  431.                             set subcont [string range $filecont [expr {[lindex $href 1] + 1}] end]
  432.                         }
  433.                         if {[regexp -indices -- $commEnd $subcont cend] &&
  434.                         [expr {[string length $subcont] - [lindex $cend 0]}] > $limit} {
  435.                             puts -nonewline $tempfid [string range $filecont 0 [expr {[lindex $href 1] + [lindex $cend 1] - 1}]]
  436.                             set filecont [string range $filecont [expr {[lindex $href 1] + [lindex $cend 1]}] end]
  437.                             set comment 0
  438.                             continue
  439.                         } else {
  440.                             set comment 1
  441.                             break
  442.                         }
  443.                     }
  444.                     
  445.                     set urltxt [string trim [string range $filecont [lindex $url 0] [lindex $url 1]] "\"' \t\r\n"]
  446.                     # No need to update links beginning with a /
  447.                     if {[string index $urltxt 0] == "/"} {
  448.                         puts -nonewline $tempfid [string range $filecont 0 [lindex $url 1]]
  449.                         set filecont [string range $filecont [expr {[lindex $url 1] + 1}] end]
  450.                         continue
  451.                     }    
  452.                     set anchor ""
  453.                     regexp {[^#]*(#.*)} $urltxt a anchor
  454.                     set urltxt [html::URLunEscape $urltxt]
  455.                     if {[catch {lindex [html::PathToFile $base $path $epath $hpPath $urltxt] 0} topath]} {set topath ""}
  456.                     # Ignore anchors if not moved and BASE.
  457.                     # Is the link pointing to a previously moved file?
  458.                     if {[set mvind [lsearch -exact $movedFiles $topath]] >= 0} {
  459.                         set topath [lindex $movedFiles2 $mvind]
  460.                         if {!$hasBase && [string index $urltxt 0] == "#"} {set topath ""}
  461.                     } elseif {[string index $urltxt 0] == "#"} {
  462.                         set topath ""
  463.                     }
  464.                         
  465.                     if {$hasBase && [regexp -nocase -indices $expBase $filecont thisLine] \
  466.                     && [regexp -nocase $expBase2 [string range $filecont [lindex $thisLine 0] [lindex $thisLine 1]]]\
  467.                     && [lindex $thisLine 0] < [lindex $url 0] && [lindex $thisLine 1] > [lindex $url 1]} {
  468.                         set topath ""
  469.                     }
  470.                     if {[string length $topath]} {
  471.                         set lnk [html::BASEfromPath $topath]
  472.                         if {!$hasBase} {
  473.                             set lnk1 [html::BASEfromPath $f]
  474.                             set path2 [lindex $lnk1 1]
  475.                             set epath2 [lindex $lnk1 2]
  476.                         } else {
  477.                             set path2 $path
  478.                             set epath2 $epath
  479.                         }
  480.                         if {$base == [lindex $lnk 0]} {
  481.                             set newurl [html::RelativePath "$path2$epath2" "[lindex $lnk 1][lindex $lnk 2]"]
  482.                         } else {
  483.                             set newurl [join [lrange $lnk 0 2] ""]
  484.                         }
  485.                         append newurl $anchor
  486.                     } elseif {!$hasBase && ($urltxt == ".." || [string range $urltxt 0 2] == "../")} {
  487.                         # Special case with relative links outside home page.
  488.                         set urlspl [split $urltxt /]
  489.                         set old [split $oldfile ${file::separator}]
  490.                         set new [split $f ${file::separator}]
  491.                         if {[llength $new] > [llength $old]} {
  492.                             set newurl ""
  493.                             for {set i 0} {$i < [expr {[llength $new] - [llength $old]}]} {incr i} {
  494.                                 append newurl "../"
  495.                             }
  496.                             append newurl $urltxt
  497.                         } else {
  498.                             set ok 1
  499.                             for {set i 0} {$i < [expr {[llength $old] - [llength $new]}]} {incr i} {
  500.                                 if {[lindex $urlspl $i] != ".."} {set ok 0}
  501.                             }
  502.                             if {$ok} {
  503.                                 set newurl "[join [lrange $urlspl [expr {[llength $old] - [llength $new]}] end] /]$anchor"
  504.                             } else {
  505.                                 set newurl $urltxt
  506.                             }
  507.                         }
  508.                     } else {
  509.                         set newurl $urltxt
  510.                     }
  511.                     puts -nonewline $tempfid [string range $filecont 0 [expr {[lindex $url 0] - 1}]]
  512.                     puts -nonewline $tempfid "\"[html::URLescape2 $newurl]\""
  513.                     set filecont [string range $filecont [expr {[lindex $url 1] + 1}] end]
  514.                 }
  515.                 if {![eof $fid]} {
  516.                     puts -nonewline $tempfid [string range $filecont 0 [expr {[string length $filecont] - 301}]]
  517.                     set filecont "[string range $filecont [expr {[string length $filecont] - 300}] end][read $fid 16384]"
  518.                     set limit [expr {[eof $fid] ? 0 : 300}] 
  519.                 } else {
  520.                     break
  521.                 }
  522.             }
  523.             puts -nonewline $tempfid $filecont
  524.         }
  525.         close $fid
  526.         close $tempfid
  527.         if {[catch {file delete $f}] && [file exists $f]} {
  528.             alertnote "Could not update [file tail $f]. An error occurred."
  529.         } else {
  530.             catch {file copy [lindex $temp 1] $f; setFileInfo $f created $created}
  531.         }
  532.         catch {file delete [lindex $temp 1]}
  533.         catch {file delete $tempf}
  534.     }
  535.     return $num
  536. }
  537.  
  538. # Updates include links to moved files in include folder or include links in moved files.
  539. proc html::UpdateAfterMove3 {movedFiles movedFiles2 homepage inclFldr allFiles {inmoved 0}} {
  540.     set num 0
  541.     set changed ""
  542.     set fid0 [open $allFiles]
  543.  
  544.     while {![eof $fid0]} {
  545.         gets $fid0 fil
  546.         if {$fil == "" || [catch {open $fil} fid]} {continue}
  547.         set filecont [read $fid 16384]
  548.         set limit [expr {[eof $fid] ? 0 : 300}]
  549.         message "Looking at [file tail $fil]…"
  550.         getFileInfo $fil finfo
  551.         set created $finfo(created)
  552.         regsub -all "\n\r" $filecont "\r" filecont
  553.         set temp [html::OpenAfile]
  554.         set tmpfid [lindex $temp 0]
  555.         set ismod 0
  556.         while {1} {
  557.             while {[regexp -nocase -indices {<!--[ \t\r\n]+#INCLUDE[ \t\r\n]+[^>]+>} $filecont res] &&
  558.             [expr {[string length $filecont] - [lindex $res 0]}] > $limit} {
  559.                 set link [string range $filecont [lindex $res 0] [lindex $res 1]]
  560.                 if {[regexp -nocase -indices {(FILE|PATH|INCLPATH)=\"[^\"]+\"} $link res1]} {
  561.                     if {!$inmoved && [set ind [lsearch -exact $movedFiles [html::ResolveInclPath \
  562.                       [string range $link [lindex $res1 0] [lindex $res1 1]] $inclFldr [file dirname $fil]]]] >= 0} {
  563.                         puts -nonewline $tmpfid [string range $filecont 0 [expr {[lindex $res 0] + [lindex $res1 0] - 1}]]
  564.                         puts -nonewline $tmpfid [html::ConvertInclPath [lindex $movedFiles2 $ind] $inclFldr $fil]
  565.                         puts -nonewline $tmpfid [string range $filecont [expr {[lindex $res 0] + [lindex $res1 1] + 1}] [lindex $res 1]]
  566.                         set ismod 1
  567.                         message "Modifying [file tail $fil]…"
  568.                     } elseif {$inmoved} {
  569.                         set ind [lsearch -exact $movedFiles2 $fil]
  570.                         set inpath [html::ResolveInclPath [string range $link [lindex $res1 0] [lindex $res1 1]] $inclFldr \
  571.                           [file dirname [lindex $movedFiles $ind]]]
  572.                         puts -nonewline $tmpfid [string range $filecont 0 [expr {[lindex $res 0] + [lindex $res1 0] - 1}]]
  573.                         puts -nonewline $tmpfid [html::ConvertInclPath $inpath $inclFldr $fil]
  574.                         puts -nonewline $tmpfid [string range $filecont [expr {[lindex $res 0] + [lindex $res1 1] + 1}] [lindex $res 1]]
  575.                         set ismod 1
  576.                         message "Modifying [file tail $fil]…"
  577.                     } else {
  578.                         puts -nonewline $tmpfid [string range $filecont 0 [lindex $res 1]]    
  579.                     }
  580.                 } else {
  581.                     puts -nonewline $tmpfid [string range $filecont 0 [lindex $res 1]]
  582.                 }
  583.                 set filecont [string range $filecont [expr {[lindex $res 1] + 1}] end]
  584.             }
  585.             if {![eof $fid]} {
  586.                 puts -nonewline $tmpfid [string range $filecont 0 [expr {[string length $filecont] - 301}]]
  587.                 set filecont "[string range $filecont [expr {[string length $filecont] - 300}] end][read $fid 16384]"
  588.                 set limit [expr {[eof $fid] ? 0 : 300}]
  589.             } else {
  590.                 break
  591.             }
  592.         }
  593.         puts -nonewline $tmpfid $filecont
  594.         close $tmpfid
  595.         close $fid
  596.         if {$ismod} {
  597.             if {[catch {file delete $fil}] && [file exists $fil]} {
  598.                 alertnote "Could not update [file tail $fil]. An error occurred."
  599.             } else {
  600.                 catch {file copy [lindex $temp 1] $fil; setFileInfo $fil created $created}
  601.             }
  602.             incr num
  603.             foreach w [html::AllWindowPaths] {
  604.                 if {[stripNameCount $w] == "$fil"} {
  605.                     lappend changed $w
  606.                 }
  607.             }
  608.         }
  609.         catch {file delete [lindex $temp 1]}
  610.     }
  611.     close $fid0
  612.     catch {file delete $allFiles}
  613.     return [list $num $changed]
  614. }
  615.  
  616.